package com.buss.product.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.product.dao.CatePropDao;
import com.buss.product.model.CateProp;
import com.buss.product.model.CateProp.PropEntryWay;
import com.buss.product.model.CateProp.PropSelectType;

@Repository
public class CatePropDaoImpl implements CatePropDao {

	@Autowired
	private BaseDao<CateProp> baseDao;
	
	@Override
	public CateProp saveProp(CateProp prop) {		
		return baseDao.saveOrEdit(prop);
	}

	@Override
	public void editProp(CateProp prop) {
		baseDao.edit(prop);
	}

	@Override
	public void removeProp(Long[] ids) {
		String sql = "delete from CateProp prop where prop.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public CateProp loadProp(Long id) {
		return baseDao.get(id, CateProp.class);
	}
	
	public CateProp loadProp(String companyId,String code){
		String slq = "select prop from CateProp prop where prop.propGroup.companyId = :companyId " +
				"and prop.code = :code";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("code", code);
		return baseDao.loadHsql(slq, params);
	}
	
	public Long getRowCount(String companyId,String code){
		String sql = "select count(prop) from CateProp prop " +
				"where prop.propGroup.companyId = :companyId and prop.code = :code";
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("code", code);
		Long cnt = (Long)baseDao.loadSingleValue(sql, params);
		return cnt;
	}
	
	@Override
	public ResultList<CateProp> queryProp(Long groupId, 
			String name, Boolean isSearch, RecordStatus status,
			OrderBy orderBy, Paginalbe page) {
		String sql = "select prop from CateProp prop where prop.propGroup.id = :groupId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("groupId", groupId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and prop.name like :name ";
			params.put("name", name);
		}
		if(isSearch != null){
			sql += " and prop.isSearch = :isSearch ";
			params.put("isSearch", isSearch);
		}
		if(status != null){
			sql += " and prop.status = :status ";
			params.put("status", status);
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.DESC);
		}
		sql += orderBy.aggOrder("prop");
		return baseDao.queryHsql(sql, params, page);
	}

	@Override
	public ResultList<CateProp> queryProp(Long[] groupIds, RecordStatus status,
			PropSelectType selectType, PropEntryWay entryWay, OrderBy orderBy) {
		String sql = "select prop from CateProp prop where prop.propGroup.id in (:groupIds) ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("groupIds", groupIds);
		if(status != null){
			sql += " and prop.status = :status ";
			params.put("status", status);
		}
		if(selectType != null){
			sql += " and prop.selectType = :selectType ";
			params.put("selectType", selectType);
		}
		if(entryWay != null){
			sql += " and prop.entryWay = :entryWay ";
			params.put("entryWay", entryWay);
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", OrderBy.DESC);
		}
		sql += orderBy.aggOrder("prop");
		return baseDao.queryHsql(sql, params, null);
	}

}
